package Thread;

public class ThreadDemo3 {

    //有三个线程，控制三个线程分别按照 cba 的顺序打印出来
    public static void main(String[] args) throws InterruptedException{
        /*Object locker1 = new Object();
        Object locker2 = new Object();

        Thread a = new Thread(() -> {
            System.out.print('c');
            synchronized (locker1) {
                //告知线程2b可以打印了
                locker1.notify();
            }
        });

        Thread b = new Thread(() -> {
            //等待线程1打印完a再打印b
            synchronized (locker1) {
                try {
                    locker1.wait();//等待
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.print('b');
            synchronized (locker2) {
                //告知线程3c可以打印了
                locker2.notify();
            }
        });

        Thread c = new Thread(() -> {
            synchronized (locker2) {
                try {
                    locker2.wait();//等待
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.print('a');
        });

        a.start();
        b.start();
        c.start();*/

        Object locker1 = new Object();
        Object locker2 = new Object();

        Thread a = new Thread(() -> {
            System.out.print('c');
            synchronized (locker1) {
                //告知线程2b可以打印了
                locker1.notify();
            }
        });

        Thread b = new Thread(() -> {
            //等待线程1打印完a再打印b
            synchronized (locker1) {
                try {
                    locker1.wait();//等待
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.print('b');
            synchronized (locker2) {
                //告知线程3c可以打印了
                locker2.notify();
            }
        });

        Thread c = new Thread(() -> {
            synchronized (locker2) {
                try {
                    locker2.wait();//等待
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.print('a');
        });

        a.start();
        b.start();
        c.start();
    }
}
